package com.kgis.fm.business.command
{

	import com.kgis.flexutil.events.MethodReturnEvent;
	import com.kgis.fm.business.event.ConnectivityEvent;
	import com.kgis.fm.business.event.FmMetaDataEvent;
	import com.kgis.fm.business.event.RequestFormEvent;
	import com.kgis.fm.common.event.CommonEntityEvent;
	import com.kgis.fm.flex.model.business.KeyObject;
	import com.kgis.mvc.ViewRegistry;
	
	import flash.events.EventDispatcher;
	
	import fm.kgis.fm.service.ServiceManager;
	
	import mx.collections.ArrayCollection;

	[Event(name="{com.kgis.fm.business.event.FmMetaDataEvent.RETURN_ID}", type="com.kgis.fm.business.event.FmMetaDataEvent")]
	[Event(name="{com.kgis.fm.business.event.ConnectivityEvent.RETURN_ID}", type="com.kgis.fm.business.event.ConnectivityEvent")]
	[Event(name="{com.kgis.fm.business.event.RequestFormEvent.RETURN_ID}", type="com.kgis.fm.business.event.RequestFormEvent")]
	[Event(name="{com.kgis.fm.common.event.CommonEntityEvent.RETURN_ID}", type="com.kgis.fm.common.event.CommonEntityEvent")]
	public class BusinessCommand extends EventDispatcher
	{

		private static function prepareParamArray(args:Array):Array
		{
			var result:Array=args;
			if (result == null)
			{
				return [null];
			}
			else
			{
				result.push(null);
				return result;
			}
		}

		[RequestHandler("{com.kgis.fm.business.event.FmMetaDataEvent.REQUEST_ID}")]
		public static function onRequestStageMenuDataEvent(request:FmMetaDataEvent):void
		{
			switch (request.actionType)
			{
				case FmMetaDataEvent.TYPE_GET_FMMETADATA:
					ServiceManager.getFmMetaDataManagementService().findAllMetadata(onOperationReturn, request);
					break;
			}
		}
		
		[RequestHandler("{com.kgis.fm.business.event.ConnectivityEvent.REQUEST_ID}")]
		public static function onRequestConnectivityDataEvent(request:ConnectivityEvent):void
		{
			switch (request.actionType)
			{
				case ConnectivityEvent.TYPE_FINDFEATURECONNECTIVITYLIST:
					ServiceManager.getConnectivityManagementService().findFeatureConnectivityList(request.spec[0] as ArrayCollection,onOperationReturn, request);
					break;
			}
		}

		[RequestHandler("{com.kgis.fm.business.event.RequestFormEvent.REQUEST_ID}")]
		public static function onRequestFormEvent(request:RequestFormEvent):void
		{
			switch (request.actionType)
			{
				case RequestFormEvent.TYPE_NEW_ENTITY:
					ServiceManager.getCommonManagementService().newEntityByEntityTypeId((request.spec as Array)[0], onOperationReturn, request);
					break;
				case RequestFormEvent.TYPE_SAVE_ENTITY:
					ServiceManager.getCommonManagementService().save((request.spec as Array)[0], onOperationReturn, request);
					break;
			}
		}
		
		[RequestHandler("{com.kgis.fm.common.event.CommonEntityEvent.REQUEST_ID}")]
		public static function onCommonEntityEvent(request:CommonEntityEvent):void {
			switch (request.actionType) {
				
				case CommonEntityEvent.TYPE_SAVE_GEOMETRYENTITY:
					var entity:* = (request.spec as Array)[0];
					var entityParams:ArrayCollection = (request.spec as Array)[1];
					ServiceManager.getCommonManagementService().saveEntityParamWithRelations(entity, entityParams, onOperationReturn, request);
					break;
				case CommonEntityEvent.TYPE_REMOVE_SPATIAL_ONLY:
					var deleteEntity:* = (request.spec as Array)[0];
					ServiceManager.getCommonManagementService().removeSpatialOnly(deleteEntity, onOperationReturn, request);
					break;
				case CommonEntityEvent.TYPE_REMOVE_FEATURE:
					var deleteFeature:* = (request.spec as Array)[0];
					ServiceManager.getCommonManagementService().remove(deleteFeature, onOperationReturn, request);
					break;
				case CommonEntityEvent.TYPE_NEW_FEATURE:
					var newEntity:* = (request.spec as Array)[0];
					ServiceManager.getCommonManagementService().create(newEntity, onOperationReturn, request);
					break;
				case CommonEntityEvent.TYPE_REMOVE_ENTITYLIST:
					ServiceManager.getCommonManagementService().removeList((request.spec as ArrayCollection), onOperationReturn, request);
					break;
				
				case CommonEntityEvent.TYPE_UPDATE_ENTITYLIST:
					var entitys:ArrayCollection = (request.spec as Array)[0] as ArrayCollection;
					ServiceManager.getCommonManagementService().updateList(entitys, onOperationReturn, request);
					break;
			}
			
		}
		
		private static function onOperationReturn(event:MethodReturnEvent):void
		{
			ServiceManager.onOperationReturn(getInstance(), event);
		}
		
		
		private static var _instance:BusinessCommand=null;

		private static function getInstance():BusinessCommand
		{
			if (_instance == null)
			{
				_instance=new BusinessCommand();
				ViewRegistry.instance.eventBus.addParticipant(_instance, true);
			}

			return _instance;
		}

	}
}